import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 路由懒加载
const Login = () => import('../views/Login.vue')
const Register = () => import('../views/Register.vue')
const Home = () => import('../views/Home.vue')
const Welcome = () => import('../views/Welcome.vue')
const UserInfo = () => import('../views/user/UserInfo.vue')
const ExhibitionList = () => import('../views/exhibition/ExhibitionList.vue')
const ExhibitionDetail = () => import('../views/exhibition/ExhibitionDetail.vue')
const GuideList = () => import('../views/guide/GuideList.vue')
const ReservationList = () => import('../views/reservation/ReservationList.vue')
const MessageBoard = () => import('../views/message/MessageBoard.vue')
const AiChat = () => import('../views/chat/AiChat.vue')

// 管理员页面
const AdminHome = () => import('../views/admin/AdminHome.vue')
const UserManage = () => import('../views/admin/UserManage.vue')
const ExhibitionManage = () => import('../views/admin/ExhibitionManage.vue')
const GuideManage = () => import('../views/admin/GuideManage.vue')
const CollectionManage = () => import('../views/admin/CollectionManage.vue')
const AnnouncementManage = () => import('../views/admin/AnnouncementManage.vue')
const ReservationManage = () => import('../views/admin/ReservationManage.vue')
const DataAnalysis = () => import('../views/admin/DataAnalysis.vue')
const MessageManage = () => import('../views/admin/MessageManage.vue')
const AiChat1 = () => import('../views/admin/AiChat1.vue')
const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: Login
    },
    {
        path: '/register',
        component: Register
    },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [
            {
                path: '/welcome',
                component: Welcome
            },
            {
                path: '/userinfo',
                component: UserInfo
            },
            {
                path: '/exhibition/list',
                component: ExhibitionList
            },
            {
                path: '/exhibition/detail/:id',
                component: ExhibitionDetail
            },
            {
                path: '/guide/list',
                component: GuideList
            },
            {
                path: '/reservation/list',
                component: ReservationList
            },
            {
                path: '/message/board',
                component: MessageBoard
            },
            {
                path: '/chat',
                component: AiChat
            }
        ]
    },
    {
        path: '/admin',
        component: AdminHome,
        redirect: '/admin/analysis',
        children: [
            // {
            //   path: '/admin/welcome',
            //   component: Welcome
            // },
            {
                path: '/admin/analysis',
                component: DataAnalysis
            },
            {
                path: '/admin/user',
                component: UserManage
            },
            {
                path: '/admin/exhibition',
                component: ExhibitionManage
            },
            {
                path: '/admin/guide',
                component: GuideManage
            },
            {
                path: '/admin/collection',
                component: CollectionManage
            },
            {
                path: '/admin/announcement',
                component: AnnouncementManage
            },
            {
                path: '/admin/reservation',
                component: ReservationManage
            },
            {
                path: '/admin/message',
                component: MessageManage
            },
            {
                path: '/admin/AiChat1',
                component: AiChat1
            }
        ]
    }
]

const router = new VueRouter({
    routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
    // 如果访问登录页或注册页，直接放行
    if (to.path === '/login' || to.path === '/register') {
        return next()
    }
    // 获取token
    const token = localStorage.getItem('token')
    if (!token) {
        return next('/login')
    }
    // 获取用户角色，判断是否有权限访问管理员页面
    if (to.path.startsWith('/admin')) {
        const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
        if (userInfo.role !== 'ADMIN') {
            return next('/home')
        }
    }
    next()
})

export default router